package com.jokelandave.sportzbetz.client.presenters;

import com.google.api.gwt.services.plus.shared.model.Person;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.jokelandave.sportzbetz.client.events.HomeNavigateEvent;
import com.jokelandave.sportzbetz.client.events.ProfileNavigateEvent;
import com.jokelandave.sportzbetz.client.events.WelcomeNavigateEvent;

public class MenuPresenter implements Presenter {

	public interface IMenuView
	{
		HasClickHandlers getProfileButton();
		HasClickHandlers getAccoutButton();
		HasClickHandlers getHomeButton();
		HasClickHandlers getGroupsButton();
		HasClickHandlers getLogoutButton();
		HasText getUserNameLabel();
		Widget asWidget();
	}
	
	private EventBus eventBus;
	private IMenuView view;
	private Person currentlyLoggedInUser;
	
	public MenuPresenter(Person currentlyLoggedInUser, IMenuView view, EventBus eventBus)
	{
		this.eventBus = eventBus;
		this.view = view;
		this.currentlyLoggedInUser = currentlyLoggedInUser;
	}

	public void bind()
	{
		view.getHomeButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) 
			{
				eventBus.fireEvent(new HomeNavigateEvent());
			}
			
		});
		
		view.getProfileButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) 
			{
				eventBus.fireEvent(new ProfileNavigateEvent());
			}
			
		});
		
		view.getUserNameLabel().setText(currentlyLoggedInUser.getDisplayName());
		
		view.getLogoutButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) 
			{
//				Window.Location.replace(logoutUrl);
				eventBus.fireEvent(new WelcomeNavigateEvent());			}
			
		});
	}
	
	@Override
	public void go(HasWidgets container) 
	{
		bind();
		container.clear();
		container.add(view.asWidget());
	}

}
